package com.leo.erp.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.erp.dao.RoleMapper;
import com.leo.erp.dto.RoleExecution;
import com.leo.erp.entity.Role;
import com.leo.erp.entity.User;
import com.leo.erp.enums.RoleStateEnum;
import com.leo.erp.service.LogService;
import com.leo.erp.service.RoleService;
import com.leo.erp.util.GlobalRuntimeException;

@Service
public class RoleServiceImpl implements RoleService{
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private LogService logService;

	@Override
	public RoleExecution deleteRoleById(String ids) {
		
		logService.insertLog("角色", "删除", ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		
		RoleExecution re = new RoleExecution();
		try {
			int effectedNum = roleMapper.deleteById(ids);
			if(effectedNum > 0) {
				re.setState(RoleStateEnum.SUCCESS.getState());
			}else {
				re.setState(RoleStateEnum.OFFLINE.getState());
			}		
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return re;
	}

	private Integer[] strArrayToIntArray(String[] idArray) {
		Integer[] b = new Integer[idArray.length];
		    for (int i = 0; i < idArray.length; i++) {
		        b[i] = Integer.parseInt(idArray[i]);
		    }
		    return b;
	}

	@Override
	public RoleExecution getRoleList(String name, int currentPage, int pageSize) {
		QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
		if(name != null && name != "") {
			queryWrapper.like("name", name);
		}
		queryWrapper.eq("deleteFlag", 0);
		
		RoleExecution re = new RoleExecution();
		
		Page<Role> page = new Page<>(currentPage, pageSize);//分页查询
		IPage<Map<String, Object>> iPage = roleMapper.selectMapsPage(page, queryWrapper);
		if(iPage.getRecords().size() > 0) {
			re.setState(RoleStateEnum.SUCCESS.getState());
			re.setPages(page.getPages());//总页数
			re.setTotal(iPage.getTotal());//总记录数
			
			re.setRolePageList(iPage.getRecords());
		}else{
			re.setState(RoleStateEnum.OFFLINE.getState());
		}
		return re;
	}

	@Override
	@Transactional
	public RoleExecution addRole(Role role, HttpServletRequest request) throws GlobalRuntimeException {
		RoleExecution re = new RoleExecution();
		try {
			int effectedNum = roleMapper.insert(role);
			if(effectedNum > 0) {
				logService.insertLog("角色", "新增" + role.getName().toString(), request);
				re.setState(RoleStateEnum.SUCCESS.getState());
			}else {
				re.setState(RoleStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		return re;
	}

	@Override
	public RoleExecution checkIsNameExist(String name) {
		RoleExecution re = new RoleExecution();
		QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("name", name);
		List<Role> list = roleMapper.selectList(queryWrapper);
		if(list.size() > 0) {
			re.setState(RoleStateEnum.SUCCESS.getState());//存在
		}else {
			re.setState(RoleStateEnum.OFFLINE.getState());//不存在
		}
		return re;
	}

	@Override
	public RoleExecution updateRole(int roleId, String name) {
		Role role = new Role();
		RoleExecution re = new RoleExecution();
		//获取session
		HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
		User user = (User) request.getSession().getAttribute("user");
		
		role.setTenantId(user.getTenantId());
		role.setRoleId(roleId);
		role.setName(name);
		
		try {
			int effectedNum = roleMapper.updateById(role);
			if(effectedNum > 0) {
				logService.insertLog("角色名称", "更改为" + role.getName(), request);
				re.setState(RoleStateEnum.SUCCESS.getState());
			}else {
				re.setState(RoleStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		return re;
	}

	@Override
	public RoleExecution findUserRole() {
		RoleExecution re = new RoleExecution();
		QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("deleteFlag", "1");
		queryWrapper.orderByAsc("roleId");
		
		try {
			List<Role> list = roleMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				re.setState(RoleStateEnum.SUCCESS.getState());
				re.setRoleList(list);
			}else {
				re.setState(RoleStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return re;
	}

	

	

}
